package demo;

/**
 *  环形链表 II
 */
class Solution21 {
	public ListNode detectCycle(ListNode head) {
		ListNode slow = head;
		ListNode fast = head;
		while (fast != null && fast.next != null) {
			slow = slow.next;
			fast = fast.next.next;
			if (slow == fast) {
				ListNode pa = slow;
				ListNode pb = head;
				while (pa != pb) {
					pa = pa.next;
					pb = pb.next;
				}
				return pa;
			}
		}
		return null;
	}
}

